﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using NetDon.OA.Interface.Common;
using NetDon.Utilities;
using NetDon.WebControls;

namespace NetDon.OA.Portal.Admin
{
    public class VehicleModelPresenter : Presenter<IVehicleModelView>
    {
        public override void OnViewInitialized()
        {
            PopulateVehicleModels();
        }

   
        public override void OnViewLoaded()
        {
            View.RefereshVechicleModels +=new EventHandler(View_RefereshVechicleModels); 
            View.RemoveVechicleModel+=new EventHandler<EventArgs<Guid>>(View_RemoveVechicleModel); 
        }


        void View_RefereshVechicleModels(object sender, EventArgs e)
        {
            PopulateVehicleModels();
        }

        void View_RemoveVechicleModel(object sender, EventArgs<Guid> e)
        {
            var service = ServiceContainer.GetService<IVehicleModelService>();
            if (service.CheckVechicleModelReferenced(e.Data))
            {
                View.Alert("该车型已经被调车信息使用，不可以删除。");
                return;
            }
            if (service.RemoveVechicleModel(e.Data))
            {
                PopulateVehicleModels();
            }
        }

        private void PopulateVehicleModels()
        {
            var service = ServiceContainer.GetService<IVehicleModelService>();
            View.PopulateVechicleModels(service.GetVechicleModels());
        }
    }
}
